const router = require('koa-router')()
const sqlQuery = require('../db')
let moment = require('../util/format');
router.prefix('/home')




router.post("/Addtextprop", async (ctx, next) => {
  try {
    let { img, comment, date, video_id } = ctx.request.body;
    let sql = "insert into textprop(img,comment,date,video_id)values(?,?,?,?)";
    let result = await sqlQuery(sql, [img, comment, date, video_id]);
    ctx.body = {
      status: 1,
      message: result
    }
  } catch (error) {
    next(error);
  }

});

router.put("/updatenumber", async (ctx, next) => {
  try {
    let { id, click } = ctx.request.body;
    if (click == 0) {
      sql = "update textprop set images=1,click=1, number=number+1 where id=?"
    } else if (click == 1) {
      sql = "update textprop set images=0,click=0, number=number-1 where id=?"
    }
    let result = await sqlQuery(sql, [id, click]);
    ctx.body = {
      status: 1,
      message: result
    }
  } catch (error) {
    next(error);
  }

});

router.put("/updatevideo", async (ctx, next) => {
  try {
    let { h_image, id, user_id, click } = ctx.request.body;
    console.log(click);
    let sql = "update happy_video  set h_image=?  where id =?";
    let result = await sqlQuery(sql, [h_image, id]);
    sql = "select * from happy_video where id=?"
    let res = await sqlQuery(sql, [id]);
    if (h_image == 1) {
      sql = "insert into praise(user_id,video_id,other_user_id)values(?,?,?)"
    } else if (h_image == 0) {
      sql = "delete from praise where user_id=? and video_id= ? and other_user_id=?"
    }
    await sqlQuery(sql, [res[0].user_id, id, user_id]);
    if (click == 0) {
      sql = "update happy_video set click=1 where id=?";
    } else if (click == 1) {
      sql = "update happy_video set click=0 where id=?"
    }
    await sqlQuery(sql, [id]);
    ctx.body = {
      status: 1,
      message: result
    }
  } catch (error) {
    next(error);
  }

});

router.put("/updateprivate", async (ctx, next) => {
  try {
    let { video_id, user_id, modi } = ctx.request.body;
    let sql = "insert into private(video_id,user_id)values(?,?)";
    let result = await sqlQuery(sql, [video_id, user_id]);
    if (modi == 3) {
      sql = "update privatestate set image=4 where modi=?";
      sql = "update happy_video set state=0 where id=?"
    } else if (modi == 4) {
      sql = "update privatestate set image=3 where modi=?"
      sql = "update happy_video set state=1 where id=?"
    }
    await sqlQuery(sql, [modi, video_id]);
    ctx.body = {
      status: 1,
      message: result
    }
  } catch (error) {
    next(error);
  }

});
router.get('/textprop/:id', async (ctx, next) => {
  try {
    let { id } = ctx.params
    let sql = "select p.id,p.img,p.comment,DATE_FORMAT(p.date,'%Y-%m-%d %H:%i:%s') date,p.number,i.image,p.click from textprop as p inner join texticon as i on p.images =i.image_id where video_id=?";
    let result = await sqlQuery(sql, [id]);
    ctx.body = {
      status: 200,
      message: result
    }

  } catch (error) {
    next(error)
  }
})

router.get('/textpropnumber', async (ctx, next) => {
  try {

    let sql = "select number from textprop  ";
    let result = await sqlQuery(sql, []);
    ctx.body = {
      status: 200,
      message: result
    }

  } catch (error) {
    next(error)
  }
})

router.get('/texticon', async (ctx, next) => {
  try {

    let sql = "select * from texticon ";
    let result = await sqlQuery(sql, []);
    ctx.body = {
      status: 200,
      message: result
    }

  } catch (error) {
    next(error)
  }
})
router.get('/privatestate', async (ctx, next) => {
  try {
    let sql = "select t.image,p.modi from texticon t ,privatestate p  where t.image_id =p.image  ";
    let result = await sqlQuery(sql, []);
    ctx.body = {
      status: 200,
      message: result
    }

  } catch (error) {
    next(error)
  }
})
// 获取视频url
router.get('/getVideoUrl', async (ctx, next) => {
  try {
    let { id } = ctx.query;
    let sql = "select url from happy_video where id=?";
    let result = await sqlQuery(sql, [id]);
    ctx.body = {
      status: 200,
      message: result
    }

  } catch (error) {
    next(error)
  }
})
// 分享
router.post('/send', async (ctx, next) => {
  try {
    let { uid, oid, message, chose } = ctx.request.body;
    console.log(uid, oid, message, chose);
    uid = parseInt(uid);
    oid = parseInt(oid);
    let params = [message, uid, oid, moment.formatTime(new Date()), chose];
    await sqlQuery('insert into messages(content,userid,other_user_id,createtime,chose) values(?,?,?,?,?)', params)
    ctx.body = "成功"
  } catch (error) {
    next(error)
  }
})
router.put("/aaa", async (ctx, next) => {
  try {
    let { id } = ctx.request.body;
    for (let i = 7; i <= 84; index++) {
      sql = "update happy_video set navigator_url=../home/home where id=? "

    }
    let result = await sqlQuery(sql, [id]);
    ctx.body = {
      status: 1,
      message: result
    }
  } catch (error) {
    next(error);
  }

});
module.exports = router
